package de.jmda.core.util.xl;

import java.io.File;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;

public class StringTableReader extends AbstractWorkbookReader<String>
{
	public StringTableReader(File file) throws IOException
	{
		super(file);
	}

	public StringTableReader(String file) throws IOException
	{
		super(file);
	}

	@Override
	protected String asT(Cell cell)
	{
		return contentAsString(cell);
	}

	/**
	 * @param cell
	 * @return raw string cell content without regard to excel's internal cell
	 *         storage format, special characters might be replaced
	 */
	private String contentAsString(Cell cell)
	{
		if (cell == null)
		{
			throw new IllegalArgumentException("cell must not be 'null'");
		}

		int cellTypeBackup = cell.getCellType();

		// force cell type to be string
		cell.setCellType(Cell.CELL_TYPE_STRING);

		// read string from cell
		String result = cell.getStringCellValue();

		// restore cell type to backed up cell type
		cell.setCellType(cellTypeBackup);

		// replace special characters
		return result.replaceAll("“", "\"");
	}
}